<template>
  <el-dialog
      :model-value="dialogVisible"
      :title="props.dialogWlTitle"
      width="40%"
      @close="handleClose"
  >
    <el-form :model="form" ref="formRef" label-width="140px" :rules="rules">
      <el-form-item label="产品名称" prop="cpmc">
        <el-select v-model="form.cpmc" clearable placeholder="请选择">
          <el-option
              v-for="(item, index) in CpName"
              :key="index"
              :label="item"
              :value="item"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="批次号" prop="pch">
        <el-select v-model="form.pch" clearable placeholder="请选择">
          <el-option
              v-for="(item, index) in pchOptions"
              :key="index"
              :label="item"
              :value="item"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发货人" prop="shiperName">
        <el-input v-model="form.shiperName" clearable/>
      </el-form-item>
      <el-form-item label="发货人联系方式" prop="shiperPhone">
        <el-input v-model="form.shiperPhone" clearable/>
      </el-form-item>
      <el-form-item label="发货地址" prop="shipAddress">
        <el-input v-model="form.shipAddress" clearable/>
      </el-form-item>
      <el-form-item label="目的地" prop="destination">
        <el-input v-model="form.destination" clearable/>
      </el-form-item>
      <el-form-item label="收件人" prop="accepter">
        <el-select v-model="form.accepter" clearable placeholder="请选择">
          <el-option
              v-for="(item, index) in clientOptions"
              :key="index"
              :label="item"
              :value="item"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="运送人" prop="carrier">
        <el-input v-model="form.carrier" clearable/>
      </el-form-item>
      <el-form-item label="运输联系方式" prop="carrierPhone">
        <el-input v-model="form.carrierPhone" clearable/>
      </el-form-item>
      <el-form-item label="运输数量" prop="number">
        <el-input v-model="form.number" clearable/>
      </el-form-item>
      <el-form-item label="车牌号" prop="license">
        <el-input v-model="form.license" clearable/>
      </el-form-item>
      <el-form-item label="货运单图片上传" prop="waybillPic">
        <my-upload
            :file-type="'wlxx'"
            @get-form-pic-value="getFormPicValue"
            :attribute="'waybillPic'"
        >
        </my-upload>
      </el-form-item>
      <el-form-item label="现场图片上传" prop="cargoPic">
        <my-upload
            :file-type="'wlxx'"
            @get-form-pic-value="getFormPicValue"
            :attribute="'cargoPic'"
        >
        </my-upload>
      </el-form-item>
      <el-form-item label="发货日期" prop="deliveryTime">
        <el-date-picker
            v-model="form.deliveryTime"
            type="date"
            placeholder="选择日期"
            :size="'default'"
            clearable
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleConfirm">
          确认
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import {defineEmits, ref, defineProps, watch, onMounted} from 'vue'
import {getAllCpmc, getCpmc, getPchByCpmc} from "@/api/apiPath/twoPart.js";
import {ElNotification} from "element-plus";
import {getAllClientName} from "@/api/apiPath/fourPart.js";
import {getOptionInfo, getOptionsInfo} from "@/api/methods/initInfo.js";
import MyUpload from "@/components/commons/myUpload.vue";

const getFormPicValue = (value)=> {
  console.log(value)
  if (value.attr === 'waybillPic') {
    form.value.waybillPic = value.pic
  } else if (value.attr === 'cargoPic') {
    form.value.cargoPic = value.pic
  }
}
const props = defineProps({
  dialogWlTitle: {
    type: String,
    default: '',
    required: true
  },
  dialogWlTableValue: {
    type: Object,
    default: {}
  },
  queryForm: {
  },
  dialogVisible: {
    type: Boolean,
    default: false
  }
})
const formRef = ref(null)
const form = ref({
  cpmc: '',
  pch: '',
  shiperName: '',
  shiperPhone: '',
  shipAddress: '',
  destination: '',
  accepter: '',
  carrier: '',
  carrierPhone: '',
  number: '',
  license: '',
  courierNumber: '',
  cargoPic: '',
  waybillPic: '',
  deliveryTime: '',
})
const pchOptions = ref([])
const clientOptions = ref([])
const CpName = ref([])
onMounted(async ()=> {
  CpName.value = await getOptionInfo(getCpmc())
  getOptionsInfo(getAllClientName(), clientOptions)
  form.value = props.dialogWlTableValue
})

watch(()=> form.value.cpmc, async (cpmc)=> {
  console.log(cpmc)
  if (form.value.cpmc != null && form.value.cpmc !== "") {
    pchOptions.value = await getOptionInfo(getPchByCpmc(cpmc))
    console.log(pchOptions.value)
    form.value.pch = ''
  }
}, {deep: true, immediate: true})

const rules = ref({
  cpmc: [{
    required: true,
    message: '不能为空',
    trigger: ['blur', 'change'],
  }],
  pch: [{
    required: true,
    message: '不能为空',
    trigger: ['blur', 'change'],
  }],
  shiperName: [{
    required: true,
    message: '不能为空',
    trigger: ['blur', 'change'],
  }],
  shiperPhone: [{
    required: true,
    message: '不能为空',
    trigger: ['blur', 'change'],
  }],
  shiperAddress: [{
    required: true,
    message: '不能为空',
    trigger: ['blur', 'change'],
  }],
  destination: [{
    required: true,
    message: '不能为空',
    trigger: ['blur', 'change'],
  }],
  accepter: [{
    required: true,
    message: '不能为空',
    trigger: ['blur', 'change'],
  }],
  carrier: [{
    required: true,
    message: '不能为空',
    trigger: ['blur', 'change'],
  }],
  carrierPhone: [{
    required: true,
    message: '不能为空',
    trigger: ['blur', 'change'],
  }],
  number: [{
    required: true,
    message: '不能为空',
    trigger: ['blur', 'change'],
  }],
  license: [{
    required: true,
    message: '不能为空',
    trigger: ['blur', 'change'],
  }],
  courierNumber: [{
    required: true,
    message: '不能为空',
    trigger: ['blur', 'change'],
  }],
  deliveryTime: [{
    required: true,
    message: '不能为空',
    trigger: ['blur', 'change'],
  }],
  shipAddress: [{
    required: true,
    message: '不能为空',
    trigger: ['blur', 'change'],
  }],
})
const emits = defineEmits(['update:modelValue', 'handleConfirm'])
const handleClose = ()=> {
  emits('update:modelValue', false)
}
const handleConfirm = () => {
  console.log(form.value)
  emits("handleConfirm", {...form.value}, {...formRef.value})
}
const picureUpLoadSuccess = (e)=> {
  console.log(e)
  ElNotification({
    message: "图片上传成功！",
    type: 'success',
    duration: 1500
  })
}
const picureUpLoadFail = (e)=> {
  console.log(e)
  ElNotification({
    message: "图片上传失败~",
    type: 'error',
    duration: 1500
  })
}

</script>

<style scoped>


</style>